#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <set>

using namespace std;

int adj[10][10];
int color[10];
bool visited[10];

map<int, set<int> > adjList;
int inDegree[10];

void findCircle(int node, int n)
{
    if (visited[node]) return; 
    if (color[node] == 1) {
	cout << "Find circle in " << node << endl;
	return;
    }
    color[node] = 1;
    for (int i = 0; i < n; i++) {
	if (i == node) continue;
	if (adj[node][i]) 
            findCircle(i, n);
    }
    color[node] = 2;
    visited[node] = 1;
}

void findCircle2(int n)
{
    while(1) {
	bool find = false;
	int i = 0;
	for (i = 0; i < n; i++) {
	    if (inDegree[i] == 0) {
		inDegree[i] = -1;
		find = true;
		break;	
	    }
	}
	if (!find) {
	    cout << "Find circle" << endl;
	    return;
	}
	for (set<int>::iterator iter = adjList[i].begin(); iter != adjList[i].end(); iter++) {
            inDegree[*iter] --;
	}
        for (int i = 0; i < n; i++) 
	    cout << inDegree[i] << " ";
	cout << endl;
	getchar();
    }
}

int main(int argc, char **argv)
{
    adj[0][1] = 1;
    adj[1][2] = 1;
    adj[2][0] = 1;
    adj[3][0] = 1;
    adj[3][2] = 1;  
    findCircle(0, 4);
    adjList[0].insert(1);
    adjList[1].insert(2);
    adjList[2].insert(0);
    adjList[3].insert(0);
    adjList[3].insert(2);
    inDegree[0] = 2;
    inDegree[1] = 1;
    inDegree[2] = 2;
    inDegree[3] = 0;

    findCircle2(4);
}
